package org.qloudgen.borg.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;

import com.google.common.collect.Lists;

@SuppressWarnings({"unchecked","rawtypes"})
public class UserRightsPage extends BasePage {
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	static Log log = LogFactory.getLog(ClusterListPage.class);

	private static final long serialVersionUID = 1L;
	private transient List<FuncBean> funcs = Lists.newArrayList();
	private transient List<String> rights = Lists.newArrayList();
	private transient String userNo,userName;
	private transient PageParameters pp;
	
	public UserRightsPage(PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
		
		// get user
		userNo = this.pp.get("userNo").toString();
		userName = this.pp.get("userName").toString();
		this.add(new Label("userName",userName));
		
		// get from metadb
		DefaultImpl.getAllFuncList(funcs);
		DefaultImpl.getUserFuncRights(userNo,rights);
		
		// add group
		final CheckGroup<FuncBean> group = new CheckGroup<FuncBean>("group", new ArrayList<FuncBean>());
		group.setRenderBodyOnly(false);
		group.add(new CheckGroupSelector("groupselector"));
				
		// add form 
		Form<?> form = new Form("form") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				List<FuncBean> selectFuncs = (List<FuncBean>) group.getDefaultModelObject();
				
				if (selectFuncs == null || selectFuncs.size() == 0) {
					info(CommonValues.INFO_NO_SELECT);
					return;
				} else if (userNo == null) {
					info(CommonValues.INFO_NO_USER);
					return;
				}
				
				DefaultImpl.updateUserRights(selectFuncs,userNo);
				setResponsePage(UserListPage.class,pp );
								
			}
			
		};
		this.add(form);
		form.add(group);	
		
		// add list
		final PageableListView<FuncBean> listView =new PageableListView<FuncBean>( "rows", funcs, CommonValues.SHOW_LIST_LARGE_NUMBER) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<FuncBean> item) {
				
				final FuncBean func = (FuncBean) item.getModelObject();
				
				if (rights.contains(func.getFuncNo()))
					item.add(new RightCheck("check", item.getModel(),true).setEnabled(true));
				else
					item.add(new Check("check",item.getModel()));
				
				item.add( new Label( "app", func.getAppName()));
				item.add( new Label( "id", func.getFuncNo()));
				item.add( new Label( "name", func.getFuncName()));
				item.add( new Label( "desc", func.getFuncDesc()));
				item.add( new Label( "status", CommonValues.statusNames.get(func.getFuncStatus())));
				
			}
		};
		listView.setOutputMarkupId(true);
		group.add(listView);
		
		// add navigator
		final AjaxPagingNavigator nav = new AjaxPagingNavigator("navigator", listView);
		nav.setOutputMarkupId(true);
		group.add(nav);
		
		// add button 
		form.add(new Button("submit"));
		Button cancel = new Button ("cancel"){
			private static final long serialVersionUID = 1L;
			public void onSubmit() {
				setResponsePage(UserListPage.class,pp);
			}
		};
		form.add(cancel);
		cancel.setDefaultFormProcessing(false);	
	
	}

	private class RightCheck extends Check {

		private static final long serialVersionUID = 1L;
		private boolean flag;
		
		public RightCheck(String id, IModel model) {
			super(id, model);
		}
		
		public RightCheck(String id, IModel model, boolean flag){
			super(id, model);
			this.flag = flag;
		}
		
		protected void onComponentTag(ComponentTag tag){
			super.onComponentTag(tag);
			tag.put("checked", this.flag);
		}
	}

}